﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Xml;
using JsonFx.Json;
using System.IO;
using WOWSharp.Community;
using AuctionHouseDataLibrary;

namespace ItemDatabase
{
    public class ItemDatabaseService : IItemDatabaseService
    {
        public Item GetItem(int itemId)
        {
            using (var context = new ItemDatabaseContext())
            {
                var item = context.Items.SingleOrDefault(p => p.ItemId == itemId);

                if (item == null)
                {
                    //Globals.BackgroundDownloadThread.QueueItemUpdateList(new List<int>() {itemId});
                    //TODO: Insert empty placeholder item
                    return Item.EmptyItem(itemId);
                }
                else
                {
                    return item;
                }
            }
        }

        public List<Item> GetItems(List<int> itemIds)
        {
            List<Item> returnItems = new List<Item>();
            using (var context = new ItemDatabaseContext())
            {
                var items = context.Items.Join(itemIds, p => p.ItemId, k => k, (itemfromdb, itemidint) => itemfromdb).ToList();
                returnItems.AddRange(items);
                var foundIds = returnItems.Select(p => p.ItemId).ToList();

                // Add in the missing items now
                //Globals.BackgroundDownloadThread.QueueItemUpdateList(itemIds.Except(foundIds).ToList());
                // TODO: Insert empty placeholder items
                foreach (var itemId in itemIds.Except(foundIds))
                {
                    returnItems.Add(Item.EmptyItem(itemId));
                }
            }
            return returnItems;
        }

        public string GetImagesFolder()
        {
            return AuctionHouseDataLibrary.Globals.ImagesFolder;
        }

        public void AddOrUpdateRecurringAHScan(string realm, bool alliance, bool horde, bool neutral, int minutesBetweenScans)
        {
            using (var context = new ItemDatabaseContext())
            {
                RecurringAHScan scan = context.RecurringAHScans.SingleOrDefault(p => p.Realm == realm);
                if (scan == null)
                {
                    RecurringAHScan newScan = new RecurringAHScan()
                        {
                            Realm = realm,
                            Alliance = alliance,
                            Horde = horde,
                            Neutral = neutral,
                            MinutesBetweenScans = minutesBetweenScans,
                            LastRunDateTime = DateTime.Now.AddMinutes(minutesBetweenScans * -1),
                            NextRunDateTime = DateTime.Now
                        };
                    context.RecurringAHScans.Add(newScan);
                    context.SaveChanges();
                    Globals.Logger.InfoFormat("Added new recurring AH scan for realm {0}:{1}", realm, newScan);
                }
                else
                {
                    scan.Alliance = alliance;
                    scan.Horde = horde;
                    scan.Neutral = neutral;
                    scan.MinutesBetweenScans = minutesBetweenScans;
                    scan.NextRunDateTime = scan.LastRunDateTime.AddMinutes(minutesBetweenScans);
                    context.Entry(scan).State = EntityState.Modified;
                    context.SaveChanges();
                    Globals.Logger.InfoFormat("Modified existing AH scan for realm {0}:{1}", realm, scan);
                }
            }
        }

        public void RemoveRecurringAHScan(string realm)
        {
            using (var context = new ItemDatabaseContext())
            {
                RecurringAHScan scan = context.RecurringAHScans.SingleOrDefault(p => p.Realm == realm);
                if (scan != null)
                {
                    context.RecurringAHScans.Remove(scan);
                    context.SaveChanges();
                    Globals.Logger.InfoFormat("Removed existing AH scan for realm {0}:{1}", realm, scan);
                }
            }
        }

        public List<RecurringAHScan> ListAllRecurringAHScans()
        {
            using (var context = new ItemDatabaseContext())
            {
                return context.RecurringAHScans.ToList();
            }
        }


        public PagedData<Item> GetItemsForDisplay(string name, int page, int start, int limit, string sort, string dir)
        {
            using (var context = new ItemDatabaseContext())
            {
                return new PagedData<Item>(context.Items.AsQueryable(), page, start, limit, sort, dir, "ItemName");
            }
        }
    }
}
